<?php
namespace app\decorate\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\decorate\model\AdminNews as news_model;
use app\decorate\model\AdminNewsCate;

class AdminNews extends AdminRest{
    protected $model;
    protected $cate_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model      = new news_model();
        $this->cate_model = new AdminNewsCate();
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:文章列表
     */
    public function newsList(){
        $input = $this->_param;
        if(!empty($input['title'])){
            $dis[] = ['title','like',"%\\".$input['title']."%"];
        }
        if(!empty($input['cate_id'])){
            $dis[] = ['cate_id','=',$input['cate_id']];
        }
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data  = $this->model->newsList($dis,$input['limit']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:添加文章
     */
    public function newsAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $res   = $this->model->newsAdd($input);
        return $this->success($res);
    }


    /**
     * User: chenniang
     * Date: 2019-10-14 16:47
     * @return void
     * descption:获取分类
     */
    public function cateList(){
        $data = $this->cate_model->cateSelect(['uniacid'=>$this->_uniacid]);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:59
     * @return \think\Response
     * descption:编辑|回显文章
     */
    public function newsUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            $data = $this->model->newsUpdate(['id'=>$input['id']],$input);
        }else{
            $data = $this->model->newsInfo(['id'=>$input['id']]);
        }
        return $this->success($data);
    }

    public function diylist()
    {
        $param = $this->_param;
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['status' , '=', 1];
        if (!empty($param['keyword'])) {
            $filter[] = ['title', 'like', '%' . $param['keyword'] . '%' ];
        }
        $list = $this->model->where($filter)
            ->paginate(['list_rows' => $param['page_count'] ? $param['page_count'] : 10, 'page' => $param['page'] ? $param['page'] : 1])->toArray();

        return $this->success($list);
    }






}
